package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.api.content.model.qo.CourseBaseDTO;
import com.xuecheng.api.content.model.qo.QueryCourseModel;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.util.StringUtil;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.entity.CourseBase;
import com.xuecheng.content.mapper.CourseBaseMapper;
import com.xuecheng.content.service.CourseBaseService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

@Service
public class CourseBaseServiceImpl extends ServiceImpl< CourseBaseMapper, CourseBase >implements CourseBaseService {
    @Override
    public PageVO< CourseBaseDTO > queryCourseList(PageRequestParams params, QueryCourseModel model,Long companyId) {
        if (params.getPageNo() < 1) {
            params.setPageNo(PageRequestParams.DEFAULT_PAGE_NUM);
        }
        if (params.getPageSize() < 1) {
            params.setPageSize(PageRequestParams.DEFAULT_PAGE_SIZE);
        }

        LambdaQueryWrapper<CourseBase> queryWrapper = new LambdaQueryWrapper<>();

        // 1.2 判断条件查询数据（如果查询条件有数据，在sql中添加条）
        // 完整版
        // if (StringUtil.isNotBlank(model.getAuditStatus())) {
        //     // 添加的审核状态条件（eq）
        //     queryWrapper.eq(CourseBase::getAuditStatus, model.getAuditStatus());
        // }
        //
        //
        //
        // if (StringUtil.isNotBlank(model.getCourseName())) {
        //     // 添加的课程名称条件（like）
        //     queryWrapper.like(CourseBase::getName, model.getCourseName());
        // }

        // 简写版
        queryWrapper.eq(StringUtil.isNotBlank(model.getAuditStatus()),
                CourseBase::getAuditStatus, model.getAuditStatus());

        queryWrapper.like(StringUtil.isNotBlank(model.getCourseName()),
                CourseBase::getName, model.getCourseName());



        // 2.创建分页数据
        Page<CourseBase> page = new Page<>(params.getPageNo(),params.getPageSize());


        // 3.根据分页和查询调价查询list数据
        Page<CourseBase> pageResult = this.page(page, queryWrapper);


        List<CourseBase> records = pageResult.getRecords();


        long total = pageResult.getTotal();


        List<CourseBaseDTO> dtos = Collections.emptyList();
        if (!(CollectionUtils.isEmpty(records))) {
            dtos = CourseBaseConvert.INSTANCE.entitys2dtos(records);
        }
        // 4.将查询结果封装到PageVO中
        /*
         * PageVO构造方法：
         *   1.当前页的集合数据
         *   2.数据库中的总条数
         *   3.当前页码
         *   4.每页条数
         * */
        PageVO pageVO = new PageVO(records,total,params.getPageNo(),params.getPageSize());

        return pageVO;
    }
}
